<template>
  <div class="workshop-input flex">
    <select-tooltip
      v-model="queryParams.workshop"
      :options="workshopOptions"
      :multiple="false"
      msg="所在车间"
      @input="onSelectWorkshop"
    ></select-tooltip>
    <select-tooltip
      v-model="queryParams.dispatchLine"
      :options="dispatchLineOptions"
      :multiple="false"
      msg="调度线"
    ></select-tooltip>
  </div>
</template>
<script>
import { getWorkshops } from 'src/boot/job/process-order'
import { getDispatchingLines } from 'src/boot/system/fixed-scheduling'
import _ from 'lodash'
export default {
  name: 'workshop-input',
  props: {
    value: [Array, String, Number, Date, Object]
  },
  data () {
    return {
      queryParams: {
        workshop: '',
        dispatchLine: ''
      },
      workshopOptions: [], // 所在车间
      dispatchLineOptions: [] // 调度线
    }
  },
  watch: {
    value: {
      handler (val) {
        this.queryParams = val
      },
      deep: true,
      immediate: true
    },
    queryParams: {
      deep: true,
      handler () {
        this.$emit('input', this.queryParams)
      }
    }
  },
  mounted () {
    this.getWorkshops()
  },
  methods: {
    // 默认重置
    initData (isInit = true, params) {
      if (isInit) {
        this.queryParams.workshop = ''
        this.queryParams.dispatchLine = ''
        this.getWorkshops()
      } else {
        this.getWorkshops(params)
      }
    },
    onSelectWorkshop (val) {
      val && this.getLine()
    },
    // 获取加工车间
    getWorkshops (val = '') {
      getWorkshops().then(res => {
        this.workshopOptions = res.data
        this.$set(this.queryParams, 'workshop', _.get(this.workshopOptions[0], 'value', val))
      })
    },
    // 获取调度线
    getLine () {
      this.dispatchLineOptions = []
      this.queryParams.dispatchLine = ''
      getDispatchingLines(this.queryParams.workshop).then(res => {
        this.dispatchLineOptions = res.data.map(v => {
          const item = v
          item.label = v.dispatchingName
          item.value = v.id
          return item
        })
        this.$set(this.queryParams, 'dispatchLine', _.get(this.dispatchLineOptions[0], 'value', ''))
      })
    }
  }
}
</script>
